package com.lifetime.utils.tree;

import java.util.Map;

import javax.swing.JComponent;

import com.kingdee.bos.ctrl.swing.KDPromptSelector;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SorterItemCollection;
import com.kingdee.eas.framework.ITreeBase;
import com.kingdee.eas.framework.client.CoreUI;

/**
 * @author lifetime 2011-5-27
 * 
 */
public class TreeLazySelector implements KDPromptSelector {
	public static final String IsShowRoot = TreeLazyUI.IsShowRoot;
	public static final String UiTitle = TreeLazyUI.UiTitle;
	public static final String IsMutilSelect = TreeLazyUI.IsMutilSelect;

	protected TreeLazyUI treeLazyUI;
	protected JComponent owner;
	protected SelectorItemCollection selectorItemCollection;
	protected SorterItemCollection sorterItemCollection;
	protected ITreeBase itreeBase;
	protected Object root;
	protected Map ctx;

	public TreeLazySelector(Map ctx, ITreeBase itreeBase, JComponent owner, Object root, SelectorItemCollection selectorItemCollection,
			SorterItemCollection sorterItemCollection) {
		this.ctx = ctx;
		this.itreeBase = itreeBase;
		this.owner = owner;
		this.root = root;
		this.selectorItemCollection = selectorItemCollection;
		this.sorterItemCollection = sorterItemCollection;
	}

	public TreeLazySelector(Map ctx, ITreeBase itreeBase, JComponent owner) {
		this(ctx, itreeBase, owner, null, null, null);
	}

	public TreeLazySelector(ITreeBase itreeBase, JComponent owner) {
		this(null, itreeBase, owner, null, null, null);
	}

	public Object getData() {
		if (ctx != null && ctx.get(IsMutilSelect) == Boolean.TRUE) {
			return treeLazyUI.result;
		}
		return treeLazyUI.result[0];
	}

	public boolean isCanceled() {
		return treeLazyUI.isCancel;
	}

	public void show() {
		if (treeLazyUI == null) {
			if (owner != null && (selectorItemCollection != null || sorterItemCollection != null) && itreeBase != null && root != null && ctx != null) {
				try {
					treeLazyUI = TreeLazyUI.getInstance(owner, selectorItemCollection, sorterItemCollection, itreeBase, root, ctx);
					treeLazyUI.setSize(300, 500);
					treeLazyUI.setLocation(400,100);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if (owner != null && itreeBase != null && ctx != null) {
				try {
					treeLazyUI = TreeLazyUI.getInstance(owner, itreeBase, ctx);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if (owner != null && owner instanceof CoreUI && itreeBase != null) {
				try {
					treeLazyUI = TreeLazyUI.getInstance((CoreUI) owner, itreeBase);
					treeLazyUI.setSize(300, 500);
					treeLazyUI.setLocation(400,100);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}
		
		treeLazyUI.setVisible(true);
		
	}

	/**
	 * @return the treeLazyUI
	 */
	public TreeLazyUI getTreeLazyUI() {
		return treeLazyUI;
	}

	/**
	 * @param treeLazyUI the treeLazyUI to set
	 */
	public void setTreeLazyUI(TreeLazyUI treeLazyUI) {
		this.treeLazyUI = treeLazyUI;
	}
	
	

}
